import 'package:bilibili/basic/http/core/hi_error.dart';
import 'package:bilibili/basic/http/dao/login_dao.dart';
import 'package:bilibili/basic/util/toast.dart';
import 'package:bilibili/basic/widget/appbar.dart';
import 'package:bilibili/basic/widget/login_button.dart';
import 'package:bilibili/basic/widget/login_effect.dart';
import 'package:bilibili/basic/widget/login_input.dart';
import 'package:flutter/material.dart';

class RegisterPage extends StatefulWidget {
  VoidCallback? onJumpToLogin;
  RegisterPage({Key? key, this.onJumpToLogin}) : super(key: key);

  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  bool _protect = false;
  bool _loginEnable = false;
  String userName = "";
  String password = "";
  String rePassWord = "";
  String imooocId = "";
  String orderId = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar("注册", "登录", () {
        widget.onJumpToLogin!();
       }),
      body: GestureDetector(
        onTap: () {

        },
        child: Container(
          child: ListView(
            children: [
              LoginEffect(protect: _protect),
              LoginInput(title: "用户名", hint: "请输入用户名", onChanged: (value){
                userName = value;
                checkInput();
              }, lineStretch: false,),
              LoginInput(title: "密码", hint: "请输入密码", onChanged: (value){
                password = value;
                checkInput();
              }, focusChanged: (value){
                _protect = value;
                setState(() {});
              }, obscureText: true, lineStretch: true,),
              LoginInput(title: "确认密码", hint: "请再次输入密码", onChanged: (value){
                rePassWord = value;
                checkInput();
              }, obscureText: true, lineStretch: false,),
              LoginInput(title: "moocId", hint: "请输入慕课网用户ID", onChanged: (value){
                imooocId = value;
                checkInput();
              }, keyboardType: TextInputType.number, lineStretch: false,),
              LoginInput(title: "订单号", hint: "请输入订单号后四位", onChanged: (value){
                orderId = value;
                checkInput();
              }, keyboardType: TextInputType.number, lineStretch: false,),
              LoginButton("注册", enable: _loginEnable,)
            ],
          ),
        ),
      ),
    );
  }

  void checkInput() {
    if (userName.isNotEmpty && password.isNotEmpty
        && rePassWord.isNotEmpty
        && imooocId.isNotEmpty && orderId.isNotEmpty
    ) {
      _loginEnable = true;
    } else {
      _loginEnable = false;
    }
    setState(() {});
  }

  Widget _loginbtn() {
    return InkWell(
      onTap: () {
        if (_loginEnable) {
          _checkParams();
        } else {
          print('请补全信息');
        }
      },
      child: Text("注册"),
    );
  }

  void _send() async {
    //3616436
    //8992
    try {
      var result = await LoginDao.register(userName, password, "3616436", "8992");
      if (result['code'] == 0) {
        showToast('注册成功');
        widget.onJumpToLogin!();
      } else {
        showWarnToast(result['msg']);
      }
    } on HiNetError catch(e) {
      print(e);
    }
  }

  void _checkParams() {
    String? tips;
    if (password != rePassWord) {
      tips = '两次密码不一致';
    } else if (orderId.length != 4) {
      tips = '请输入订单号的后四位';
    }
    if (tips != null) {
      print(tips);
     return;
    }
    _send();
  }


}
